<template>
  <div class="">
    <div class="list-table linear mb15" style="">
      <ul class="moneyBox">
        <li style="max-width: 190px;">
          <p>{{ $t('出资总数') }}</p>
          <span :title="total">{{ total }}</span>
        </li>
        <!--   -->
        <li>
          <p>{{ $t('总认缴') }}<i>{{ $t('金额单位') }}</i></p>
          <span :title="moneyFormat(subscription, 'w', 6)">{{ moneyFormat(subscription, 'w', 2) }}</span>
        </li>
        <!--  -->
        <li>
          <p>{{ $t('总实缴') }}<i>{{ $t('金额单位') }}</i></p>
          <span :title="moneyFormat(paidIn, 'w', 6)">{{ moneyFormat(paidIn, 'w', 2) }}</span>
        </li>
        <!--  -->
        <li style="max-width: 220px;">
          <p>{{ $t('实缴比例') }}<i>%</i></p>
          <span>
            {{ Math.round(proportion*10000)/100 }}
          </span>
        </li>
        <!--  -->
        <li v-for="(item, index) in tempList" :key="index">
          <p v-if="item.name !== undefined">{{ $t(item.name) }}<i>{{ $t('金额单位') }}</i></p>
          <span :title="moneyFormat(item.value, 'w', 6)">{{ moneyFormat(item.value, 'w', 2) }}</span>
        </li>
      </ul>
    </div>

    <div class="list-table">
      <div class="area-header">
        <span class="area-header-title">{{ $t('出资人信息') }}</span>
      </div>
      <el-form ref="queryForm" :model="queryParams" :inline="true">
        <el-form-item :label="$t('出资人')" prop="userName">
          <el-input
            v-model="queryParams.userName"
            :placeholder="$t('出资人')"
            clearable
            @keyup.enter.native="queryHandle"
          />
        </el-form-item>
        <el-form-item :label="$t('认缴时间')" prop="timeSubscriptionArray">
          <DatePicker
            v-model="daterange"
            type="daterange"
          />

        </el-form-item>
        <el-form-item :label="$t('合伙人类型')" prop="partnerType">
          <dictSelect
            v-model="queryParams.partnerType"
            :clearable="true"
            dictionary="partner_type"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="queryHandle">{{ $t("搜索") }}</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{ $t("重置") }}</el-button>
        </el-form-item>
      </el-form>

      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            v-hasPermi="['fund:investor:edit']"
            type="primary"
            icon="el-icon-plus"
            size="mini"
            @click="addHandle"
          >{{ $t("新增") }}</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            v-hasPermi="['fund:investor:export']"
            type="warning"
            icon="el-icon-download"
            size="mini"
            @click="exportHandle"
          >{{ $t("导出") }}</el-button>
        </el-col>
      </el-row>

      <el-table v-loading="loading" tooltip-effect="darkTable" :show-summary="true" :summary-method="getSummaries" :data="fundInvestorList" border>
        <el-table-column :label="$t('序号')" type="index" align="center" show-overflow-tooltip width="50">
          <template slot-scope="scope">
            {{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}
          </template>
        </el-table-column>
        <el-table-column :label="$t('出资人')" align="center" prop="userName" :show-overflow-tooltip="true" width="250">
          <template slot-scope="scope">
            <span class="link-type" @click="updateHandle(scope.row)">{{ scope.row.name }}&nbsp;
              <span v-if="scope.row.partnerType === 'GP'" style="color: #00B83F; font-weight: bold">{{ scope.row.partnerType }}</span>
              <span v-else style="color: #d2322d; font-weight: bold">{{ scope.row.partnerType }}</span>
            </span>
          </template>
        </el-table-column>
        <el-table-column :label="$t('认缴时间')" align="center" prop="timeSubscription" show-overflow-tooltip min-width="120">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.timeSubscription) }}</span>
          </template>
        </el-table-column>
        <el-table-column :label="$t('认缴金额') + $t('金额单位')" header-align="center" align="right" prop="subscribedAmount" show-overflow-tooltip min-width="120">
          <template slot-scope="scope">
            <span class="money-text">{{ moneyFormat(scope.row.subscribedAmount, 'w') }}</span>
          </template>
        </el-table-column>
        <!--        <el-table-column :label="$t('币种')" align="center" prop="currency" :formatter="currencyFormat" show-overflow-tooltip min-width="100" />-->
        <el-table-column :label="$t('认缴比例(%)')" header-align="center" align="right" prop="subscriptionRate" show-overflow-tooltip min-width="100">
          <template slot-scope="scope">
            <span>{{ moneyFormat(scope.row.subscriptionRate) }}</span>
          </template>
        </el-table-column>
        <el-table-column :label="$t('累计实缴金额') + $t('金额单位')" header-align="center" align="right" prop="accumulatedPaidMount" show-overflow-tooltip min-width="130">
          <template slot-scope="scope">
            <span class="money-text">{{ moneyFormat(scope.row.accumulatedPaidMount, 'w') }}</span>
          </template>
        </el-table-column>
        <el-table-column :label="$t('认缴出资余额') + $t('金额单位')" header-align="center" align="right" prop="subInvestAmount" show-overflow-tooltip min-width="130">
          <template slot-scope="scope">
            <span class="money-text">{{ moneyFormat(scope.row.subInvestAmount,'w',2,true) }}</span>
          </template>
        </el-table-column>
        <el-table-column :label="$t('累计分配金额') + $t('金额单位')" header-align="center" align="right" prop="allocationAmount" show-overflow-tooltip min-width="130">
          <template slot-scope="scope">
            <span class="money-text">{{ moneyFormat(scope.row.allocationAmount, 'w') }}</span>
          </template>
        </el-table-column>
        <el-table-column :label="$t('累计管理费') + $t('金额单位')" header-align="center" align="right" prop="mangerFee" show-overflow-tooltip min-width="120">
          <template slot-scope="scope">
            <span class="money-text">{{ moneyFormat(scope.row.mangerFee,'w',2,true) }}</span>
          </template>
        </el-table-column>
        <el-table-column :label="$t('其他费用') + $t('金额单位')" header-align="center" align="right" prop="otherFee" show-overflow-tooltip min-width="120">
          <template slot-scope="scope">
            <span class="money-text">{{ moneyFormat(scope.row.otherFee,'w',2,true) }}</span>
          </template>
        </el-table-column>
        <el-table-column :label="$t('投资收益') + $t('金额单位')" header-align="center" align="right" prop="investIcomeAmount" show-overflow-tooltip min-width="120">
          <template slot-scope="scope">
            <span class="money-text">{{ moneyFormat(scope.row.investIcomeAmount,'w',2,true) }}</span>
          </template>
        </el-table-column>
        <el-table-column :label="$t('资本账户余额') + $t('金额单位')" header-align="center" align="right" prop="amountBalance" show-overflow-tooltip min-width="130">
          <template slot-scope="scope">
            <span class="money-text">{{ moneyFormat(scope.row.amountBalance,'w',2,true) }}</span>
          </template>
        </el-table-column>
        <el-table-column :label="$t('所占基金估值') + $t('金额单位')" header-align="center" align="right" prop="valuationAmount" show-overflow-tooltip min-width="130">
          <template slot-scope="scope">
            <span class="money-text">{{ moneyFormat(scope.row.valuationAmount,'w',2,true) }}</span>
          </template>
        </el-table-column>
        <el-table-column :label="$t('缴款进度(%)')" header-align="center" align="right" prop="paidInProgress" show-overflow-tooltip min-width="100">
          <template slot-scope="scope">
            <span>{{ isEmpty(scope.row.paidInProgress) ? 0 : moneyFormat(scope.row.paidInProgress * 100) }}</span>
          </template>
        </el-table-column>
        <el-table-column :label="$t('合伙协议签订时间')" align="center" prop="investmentTime" show-overflow-tooltip min-width="140">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.investmentTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column :label="$t('DPI')" align="center" prop="dpi" show-overflow-tooltip min-width="130">
          <template slot="header">
            <span>{{ $t('DPI') }}</span>
            <el-tooltip class="item" effect="dark" :content="$t('累计分配金额 ÷ 累计实缴金额')" placement="top">
              <i class="el-icon-question" />
            </el-tooltip>
          </template>
          <template slot-scope="scope">
            <span class="money-text">{{ scope.row.dpi }}</span>
          </template>
        </el-table-column>
        <el-table-column :label="$t('IRR')" align="center" prop="irr" show-overflow-tooltip>
          <template slot="header" slot-scope="{}">
            <span>{{ $t('IRR') }}</span>
            <el-tooltip :content="isEmpty(irrPointerStatus) ? $t('IRR') : $t(irrPointerStatus)">
              <i class="el-icon-question" />
            </el-tooltip>
          </template>
          <template slot-scope="scope">
            <span><el-link type="primary" @click="findIRR(scope.row)">{{ scope.row.irr }}</el-link></span>
          </template>
        </el-table-column>
        <el-table-column :label="$t('TVPI')" align="center" prop="tvpi" show-overflow-tooltip width="100">
          <template slot="header" slot-scope="{}">
            <span>{{ $t('TVPI') }}</span>
            <el-tooltip :content="$t('(累计分配金额 + 所占基金估值) / 累计实缴金额')">
              <i class="el-icon-question" />
            </el-tooltip>
          </template>
          <template slot-scope="scope">
            <span>{{ moneyFormat(scope.row.tvpi) }}</span>
          </template>
        </el-table-column>
        <el-table-column :label="$t('RVPI')" align="center" prop="rvpi" show-overflow-tooltip width="100">
          <template slot="header" slot-scope="{}">
            <span>{{ $t('RVPI') }}</span>
            <el-tooltip :content="$t('TVPI - DPI')">
              <i class="el-icon-question" />
            </el-tooltip>
          </template>
          <template slot-scope="scope">
            <span>{{ moneyFormat(scope.row.rvpi) }}</span>
          </template>
        </el-table-column>
        <el-table-column :label="$t('操作')" fixed="right" align="center" class-name="small-padding fixed-width" width="200">
          <template slot-scope="scope">
            <el-button
              v-hasPermi="['fund:investor:edit']"
              size="small"
              type="text"
              icon="el-icon-edit"
              @click="updateHandle(scope.row)"
            >{{ $t('修改') }}</el-button>
            <el-button
              v-hasPermi="['fund:investor:remove']"
              :disabled="scope.row.buttonDisabledState"
              size="small"
              type="text"
              icon="el-icon-delete"
              @click="deleteHandle(scope.row)"
            >{{ $t('删除') }}</el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />

      <!-- 添加或修改出资人信息对话框 -->
      <el-dialog :title="title" :visible.sync="investFormOpen" :close-on-click-modal="false" :lock-scroll="false" custom-class="dialogForm" append-to-body>
        <fundInvestorForm ref="fundInvestorForm" />
      </el-dialog>
    </div>
    <!-- 历史IRR列表对话框 -->
    <el-dialog :title="$t('IRR')" :visible.sync="irrOpen" :close-on-click-modal="false" width="551px" custom-class="customDialog" :lock-scroll="false" append-to-body>
      <el-table v-loading="irrLoading" :data="investIrrList" border class="nowrapTable formTable">
        <el-table-column :label="$t('序号')" align="center" width="50">
          <template slot-scope="scope">
            {{ scope.$index + 1 }}
          </template>
        </el-table-column>
        <el-table-column :label="$t('维度')" align="center" prop="dimension" width="259" />
        <el-table-column :label="$t('IRR')" align="center" prop="xirr" width="200">
          <template slot-scope="scope">
            <span><el-link type="primary" @click="findXIrrDetails(scope.row)">{{ scope.row.xirr }}</el-link></span>
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>
    <!-- xIrr计算明细对话框 -->
    <el-dialog :title="$t('计算明细')" :visible.sync="xIrrDetailsOpen" :close-on-click-modal="false" :lock-scroll="false" :width="'50%'" custom-class="customDialog" append-to-body>
      <xIrrBaseDetailsList ref="xIrrBaseDetailsList" class="dialog-content" />
      <div slot="footer" class="dialog-footer mt-15" style="text-align: right;">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="exportXIrrHandle"
        >{{ $t('导出') }}
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listInfoFundInvestor,
  delFundInvestor,
  getFundInvestorSum,
  getFundInvestorIrrHistory
} from '@/api/fund/investor/fundInvestor'
import fundInvestorForm from './fundInvestorForm.vue'
import { downLoadExcel } from '@/utils/excelDownload'
import { findFundFunds } from '@/api/fund/investor/fundInvestor'
import xIrrBaseDetailsList from '@/components/Xirr/details/xirrBaseDetailsList'
import { getSysIndicatorsConfig } from '@/api/fund/sysIndicatorsConfig/sysIndicatorsConfig'
import moment from 'moment'
// import { sumFundExpense } from '@/api/fund/expense/fundExpense'
export default {
  components: {
    fundInvestorForm,
    xIrrBaseDetailsList
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      irrLoading: false,
      // 总条数
      total: 0,
      // 总认缴
      subscription: 0,
      // 总实缴
      paidIn: 0,
      // 实缴比例
      proportion: 0,
      // 出资人信息表格数据
      fundInvestorList: [],
      fundInvestorSum: {},
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      investFormOpen: false,
      irrOpen: false,
      xIrrDetailsOpen: false,
      // 时间区间
      daterange: [],
      // 合伙人类型
      partnerTypeOptions: [],
      // 查询参数
      queryParams: {
        userName: undefined,
        subscribedAmount: undefined,
        pageNum: 1,
        pageSize: 10
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      // 币种字典
      currencyOptions: [],
      // 基金情况文字介绍
      textShowArray: [
        this.$t('未缴金额：当前基金LP总认缴-当前基金LP总实缴'),
        this.$t('在缴金额：当前基金LP总实缴'),
        this.$t('可投金额：当前基金LP总实缴 – 当前基金作为投资主体的实际投资金额')
      ],
      irrPointerStatus: '',
      tempList: [],
      // 出资人IRR列表
      investIrrList: []
    }
  },
  created() {
    this.getList()
    // this.getDicts('currency').then(response => {
    //   this.currencyOptions = response.data
    // })
    // 合伙人类型partnerTypeOptions
    this.getDicts('partner_type').then(response => {
      this.partnerTypeOptions = response.data
    })
    // 获取IRR计算方式
    getSysIndicatorsConfig().then(response => {
      console.log(response)
      if (response.data) {
        var showPoint = response.data.showPoint
        if (showPoint) {
          if (showPoint === 'SJDM') {
            var temp = moment(moment().quarter(moment().quarter() - 1).startOf('quarter').valueOf()).format('YYYY/MM')
            var split = temp.split('/')
            var year = split[0]
            var quarter = Number(split[1])
            if (quarter >= 1 && quarter <= 3) {
              quarter = 1
            } else if (quarter > 3 && quarter <= 6) {
              quarter = 2
            } else if (quarter > 6 && quarter <= 9) {
              quarter = 3
            } else {
              quarter = 4
            }
            this.irrPointerStatus = year + '-Q' + quarter
          } else {
            this.irrPointerStatus = '最新IRR'
          }
        }
      }
    })
  },
  methods: {
    /** 查询出资人信息列表 */
    getList() {
      this.loading = true
      this.queryParams.fundId = this.$route.query.id
      getFundInvestorSum(this.addDateRange(this.queryParams, this.daterange)).then(res => {
        this.fundInvestorSum = res.data
      })
      listInfoFundInvestor(
        this.addDateRange(this.queryParams, this.daterange)
      ).then(response => {
        this.fundInvestorList = response.rows
        this.total = response.total
        this.loading = false
      })
      findFundFunds(this.$route.query.id).then(response => {
        // 总实缴金额
        var accumulatedPaidMount = response.data.accumulatedPaidMount
        // 总认缴金额
        var subscribedAmount = response.data.subscribedAmount
        // 实际投资金额
        // var amountInvestmentFact = response.data.amountInvestmentFact
        // 未缴金额：当前基金LP总认缴-当前基金LP总实缴
        var temp1 = subscribedAmount - accumulatedPaidMount
        // 在缴金额：当前基金LP总实缴
        // var temp2 = accumulatedPaidMount
        // // 可投金额：当前基金LP总实缴 – 当前基金作为投资主体的实际投资金额
        // var temp3 = 0
        // sumFundExpense(this.$route.query.id).then(res => {
        // 可投金额：当前基金LP总实缴 – 当前基金作为投资主体的实际投资金额 // 改值同步至 基金资金情况饼状图 数据取值同步
        // temp3 = accumulatedPaidMount - amountInvestmentFact - Number(res.data)
        this.tempList = [
          { value: temp1, name: this.$t('未缴金额'), style: 'background: linear-gradient(to right bottom, rgb(95,90,255) 0%, rgb(159,173,241) 100%);' }
          // { value: temp2, name: this.$t('在缴金额'), style: 'background: linear-gradient(to right bottom, rgb(255,123,98) 0%, rgb(247,215,135) 100%);' },
          // { value: temp3, name: this.$t('可投金额'), style: 'background: linear-gradient(to right bottom, rgb(255, 155, 183) 0%, rgb(197, 63, 63) 100%);' }
        ]
        // })
      })
      this.getListSumMoney()
    },
    getListSumMoney() {
      listInfoFundInvestor(
        this.addDateRange({ fundId: this.$route.query.id }, [])
      ).then(response => {
        const fundInvestorList = response.rows
        // 计算顶部的四个数据
        this.subscription = 0
        this.paidIn = 0
        this.proportion = 0
        fundInvestorList.forEach(item => {
          this.subscription += item.subscribedAmount
          this.paidIn += item.accumulatedPaidMount
        })
        if (this.subscription === 0) {
          this.proportion = 0
        } else {
          this.proportion = this.paidIn / this.subscription
        }
      })
    },
    // // 币种字典回显
    // currencyFormat(row) {
    //   return this.selectDictLabel(this.currencyOptions, row.currency)
    // },
    // 合伙人类型
    partnerTypeFormat(row) {
      return this.selectDictLabel(this.partnerTypeOptions, row.partnerType)
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        type: undefined,
        name: undefined,
        investmentTime: undefined,
        timeSubscriptionArray: undefined,
        incomeDistributionMethod: undefined,
        timeSubscription: undefined,
        subscribedAmount: undefined,
        contractualRateReturn: undefined,
        yieldStructure: undefined,
        currency: undefined,
        partnerType: undefined,
        subscriptionProportion: undefined,
        accumulatedPaidMount: undefined,
        remark: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined,
        delFlag: undefined,
        roundsId: undefined,
        investorNature: undefined,
        investorMajor: undefined
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    queryHandle() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterange = []
      this.resetForm('queryForm')
      this.queryHandle()
    },
    /** 新增按钮操作 */
    addHandle() {
      this.investFormOpen = true
      this.title = this.$t(this.$t('出资人信息'))
      this.$nextTick(() => {
        this.$refs.fundInvestorForm.init(null)
      })
    },
    /** 修改按钮操作 */
    updateHandle(row) {
      this.investFormOpen = true
      this.title = this.$t(this.$t('出资人信息'))
      this.$nextTick(() => {
        // 调用form页面的 init 方法初始化数据
        this.$refs.fundInvestorForm.init(row)
      })
    },
    /** 删除按钮操作 */
    deleteHandle(row) {
      var that = this
      this.$confirm(this.$t('是否确认删除?'), this.$t('警告'), {
        confirmButtonText: this.$t('确定'),
        cancelButtonText: this.$t('取消'),
        type: 'warning'
      })
        .then(function() {
          that.loading = true
          return delFundInvestor(row.id)
        })
        .then(() => {
          this.getList()
          this.msgSuccess(this.$t('删除成功'))
        })
        .catch(function() {
          that.loading = false
        })
    },
    /** 导出按钮操作 */
    exportHandle() {
      downLoadExcel('/investor/fundInvestor/export', this.queryParams)
    },
    // 合计
    getSummaries(param) {
      const { columns } = param
      const sums = []
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = '合计'
        } else if (index === 3) {
          sums[index] = this.moneyFormat(this.fundInvestorSum.subscribedAmount, 'w')
        } else if (index === 4) {
          sums[index] = this.moneyFormat(this.fundInvestorSum.subscriptionRate)
        } else if (index === 5) {
          sums[index] = this.moneyFormat(this.fundInvestorSum.accumulatedPaidMount, 'w')
        } else if (index === 6) {
          sums[index] = this.moneyFormat(this.fundInvestorSum.subInvestAmount, 'w')
        } else if (index === 7) {
          sums[index] = this.moneyFormat(this.fundInvestorSum.allocationAmount, 'w')
        } else if (index === 8) {
          sums[index] = this.moneyFormat(this.fundInvestorSum.mangerFee, 'w')
        } else if (index === 9) {
          sums[index] = this.moneyFormat(this.fundInvestorSum.otherFee, 'w')
        } else if (index === 10) {
          sums[index] = this.moneyFormat(this.fundInvestorSum.investIcomeAmount, 'w')
        } else if (index === 11) {
          sums[index] = this.moneyFormat(this.fundInvestorSum.amountBalance, 'w')
        } else if (index === 12) {
          sums[index] = this.moneyFormat(this.fundInvestorSum.valuationAmount, 'w')
        } else if (index === 13) {
          sums[index] = this.isEmpty(this.fundInvestorSum.paidInProgress) ? 0 : this.moneyFormat(this.fundInvestorSum.paidInProgress * 100)
        } else {
          sums[index] = ''
        }
      })
      return sums
    },
    findIRR(row) {
      this.investIrrList = []
      this.irrOpen = true
      this.irrLoading = true
      getFundInvestorIrrHistory(row.id).then(response => {
        this.investIrrList = response.rows
      })
      this.irrLoading = false
    },
    findXIrrDetails(row) {
      this.xIrrDetailsOpen = true
      this.$nextTick(() => {
        this.$refs.xIrrBaseDetailsList.init(row.id)
      })
    },
    exportXIrrHandle() {
      this.$nextTick(() => {
        this.$refs.xIrrBaseDetailsList.exportHandle()
      })
    }
  }
}
</script>
<style lang="scss" scoped>
/deep/.customDialog .el-dialog__body {
  height: 48vh;
  overflow-y: auto;
  .list-table {
    margin: -20px;
  }
}
</style>
